package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.AppMenuDao;
import com.framework.entity.or.OrAppMenu;
import com.framework.entity.or.OrAppMenuType;
import com.framework.entity.sys.SysAgent;
import com.framework.exception.RRException;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.AgentService;
import com.framework.service.AppMenuService;
import com.framework.service.app.OrAppMenuTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wq
 */
@Service("appMenuService")
public class AppMenuServiceImpl extends ServiceImpl<AppMenuDao, OrAppMenu> implements AppMenuService {


    @Autowired
    private OrAppMenuTypeService orAppMenuTypeService;

    @Override
    public List<OrAppMenu> queryAppMenuList(Integer type, Long deptId) {
        if (type == 0 || type == null) {
            throw new RRException("9000", "类型不能为空");
        }
        if (deptId == 0 || deptId == null) {
            throw new RRException("9000", "所属渠道不能为空");
        }
        OrAppMenu appMenu = new OrAppMenu();
        appMenu.setDeptId(deptId);
        appMenu.setType(type);
        List<OrAppMenu> list = baseMapper.queryAppMenuList(appMenu);
        return list;
    }

    @Override
    public PageUtils queryAppMenuListForAdmin(Map<String, Object> params) {
        Long deptId = (Long) params.get("deptId");
        Integer type=0;
        if(params.get("type")!=null){
            type= Integer.valueOf(params.get("type").toString());
        }
        Page<OrAppMenu> page = selectPage(new Query<OrAppMenu>(params).getPage(),
                new EntityWrapper<OrAppMenu>().eq("dept_id", deptId)
                .eq(type!=0,"type",type)
        );
        return new PageUtils(page);
    }


    @Override
    public void updateMenuById(OrAppMenu orAppMenu) {
        baseMapper.updateById(orAppMenu);
    }

    @Override
    public List<OrAppMenuType> queryMenuGroupByType(Long deptId) {

        List<OrAppMenuType> orAppMenuTypes = orAppMenuTypeService.selectList(new EntityWrapper<OrAppMenuType>()
                .eq("dept_id", deptId)
                .eq("state",1)
                .orderBy("menu_type", true)
        );
        List<OrAppMenu> orAppMenus = baseMapper.selectList(new EntityWrapper<OrAppMenu>()
                .eq("dept_id", deptId)
                .eq("state",1)
                .orderBy("menu_id", true)
        );
        for (OrAppMenuType orAppMenuType : orAppMenuTypes) {
            orAppMenuType.setMenuList(
                    orAppMenus.stream().
                            filter(e -> orAppMenuType.getMenuType().intValue() == e.getType())
                            .collect(Collectors.toList())
            );
        }
        return orAppMenuTypes;
    }
}
